package com.jlkf.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.StringJoiner;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.OrderChargeDetailMapper;
import com.jlkf.dao.OrderItemsMapper;
import com.jlkf.dao.PinpayRecordMapper;
import com.jlkf.dao.PreAuthorizationAndCaptureSteponeMapper;
import com.jlkf.dao.StarRemarkMapper;
import com.jlkf.dao.SugFeedbackMapper;
import com.jlkf.dao.UserBrankMapper;
import com.jlkf.dao.UserCouponMapper;
import com.jlkf.dao.UserMapper;
import com.jlkf.dao.UserMessageMapper;
import com.jlkf.fatZebra.ReturnMsg;
import com.jlkf.pojo.OrderChargeDetail;
import com.jlkf.pojo.OrderItems;
import com.jlkf.pojo.PinpayRecord;
import com.jlkf.pojo.PreAuthorizationAndCaptureStepone;
import com.jlkf.pojo.StarRemark;
import com.jlkf.pojo.SugFeedback;
import com.jlkf.pojo.SysUser;
import com.jlkf.pojo.User;
import com.jlkf.pojo.UserBrank;
import com.jlkf.pojo.UserCoupon;
import com.jlkf.pojo.UserMessage;
import com.jlkf.service.SugFeedbackService;
import com.jlkf.service.util.FatZebraRequestUtils;
import com.jlkf.view.OA.OAStarRemark;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.RandomUtil;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;

import io.practiceinsight.pinpayments.pojo.ErrorMessage;
import io.practiceinsight.pinpayments.pojo.ErrorResponse400;
import io.practiceinsight.pinpayments.pojo.ErrorResponseCard;
import io.practiceinsight.pinpayments.pojo.ResponseCharges;
import io.practiceinsight.pinpayments.utils.PinPayHttpUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class SugFeedbackServiceimpl implements SugFeedbackService {

	@Autowired
	private SugFeedbackMapper sugFeedbackMapper;
	@Autowired
	private UserMessageMapper userMessageMapper;
	@Autowired
	private OrderChargeDetailMapper orderChargeDetailMapper;
	@Autowired
	private OrderItemsMapper orderItemsMapper;
	@Autowired
	private UserBrankMapper userBrankMapper;
	@Autowired
	private StarRemarkMapper starRemarkMapper;
	@Autowired
	private UserCouponMapper userCouponMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PinpayRecordMapper pinpayRecordMapper;
	@Autowired
	private PreAuthorizationAndCaptureSteponeMapper preAuthorizationAndCaptureSteponeMapper;
	
	@Override
	public Object getList(String startTime, String endTime, Long msgstat, Integer replystat, Integer pageNo,
			Integer pageSize,Integer orderId) {
		Map<String,Object> map = new HashMap<>();
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=10;
		}
		if(msgstat!=null){
			map.put("msgstat", msgstat);
		}
		if(replystat!=null){
			map.put("replystat", replystat);
		}
		 if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			 String[] startTime1= startTime.split("/");
		    	String[] endTime1= endTime.split("/");
		    	map.put("startTime", startTime1[2]+"-"+startTime1[1]+"-"+startTime1[0]);
		    	map.put("endTime", endTime1[2]+"-"+endTime1[1]+"-"+endTime1[0]);
		    }
		 if(!RegexUtil.isNull(orderId)){
			 map.put("orderId", orderId);
		 }
		 map.put("isUser", 1);
//		 map.put("identity", 2);
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<SugFeedback> list=sugFeedbackMapper.getSugFeedbackList(map);
			PageInfo<SugFeedback> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"feedback list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdate(String json) {
		SugFeedback sfb = StringTools.json2Obj(json, SugFeedback.class);
		if(sfb==null){
			return new ReturnData(201,"The parameter list is empty!");
		}
		try {
			int num=sugFeedbackMapper.updateByPrimaryKeySelective(sfb);
			if(num==0){
				return new ReturnData(202,"fail");
			}
			return new ReturnData(200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object getMessageList(String startTime, String endTime, Integer pageNo, Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
		if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			String[] startTime1= startTime.split("/");
	    	String[] endTime1= endTime.split("/");
	    	map.put("startTime", startTime1[2]+"-"+startTime1[1]+"-"+startTime1[0]);
	    	map.put("endTime", endTime1[2]+"-"+endTime1[1]+"-"+endTime1[0]);
	    }
		try {
			PageHelper.startPage(pageNo, pageSize);
			 List<UserMessage> list=userMessageMapper.getUserMessageList(map);
			 PageInfo<User> info = new PageInfo(list);
		     Integer totalPage= info.getPages();
			 Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"message list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	//清洁费
	@Override
	public Object getDriverList(String startTime, String endTime, String account, Integer msgstat, Integer pageNo,
			Integer pageSize,Integer orderId) {
		Map<String,Object> map = new HashMap<>();
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=10;
		}
		if(msgstat!=null){
			map.put("msgstat", msgstat);
		}
		if(!RegexUtil.isNull(msgstat)){
			map.put("account", msgstat);
		}
		if(!RegexUtil.isNull(orderId)){
			map.put("orderId", orderId);
		}
		 if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			    String[] startTime1= startTime.split("/");
		    	String[] endTime1= endTime.split("/");
		    	map.put("startTime", startTime1[2]+"-"+startTime1[1]+"-"+startTime1[0]);
		    	map.put("endTime", endTime1[2]+"-"+endTime1[1]+"-"+endTime1[0]);
		    }
		// map.put("identity", 2);//司机的意见反馈
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<SugFeedback> list=sugFeedbackMapper.getDriverList(map);
			PageInfo<SugFeedback> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"feedback list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object driverNoDeductions(Long id) {
		int code=200;String msg="success";
		try {
			SugFeedback sfb = sugFeedbackMapper.selectByPrimaryKey(id);
			sfb.setMsgstat(3);//已处理
			int num = sugFeedbackMapper.updateByPrimaryKeySelective(sfb);
			if(num==0){
				code=201;msg="Fail to update trip status";
			}else{
				Map<String,Object> map = new HashMap<>();
				map.put("orderId", sfb.getOrderId());
				List<OrderChargeDetail> list=orderChargeDetailMapper.getOrderChargeDetail(map);
				if(list.size()>0){
					OrderChargeDetail ocd = list.get(0);
					ocd.setCleanness(0.0);
					ocd.setCleaning_fee_audit(3);//清洁费审核不通过 不扣用户的款
					int n=orderChargeDetailMapper.updateByPrimaryKeySelective(ocd);
					if(n==0){
						code=202;msg="Fail to update trip status";
					}
				}
			}
			if(code!=200){
				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			}
			return new ReturnData(code,msg);
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
	
	@Override
	public Object deductionsUser(String json,HttpSession session) {
		SugFeedback sfb = StringTools.json2Obj(json, SugFeedback.class);
		if(sfb==null){
			return new ReturnData(201,"List must be filled");
		}
		int code=200;String msg="success";
		SysUser user = (SysUser) session.getAttribute("user");
		if(user==null){
			return new ReturnData(205,"Login expired, please log in again.");
		}	
		String jrc =null;
//		try {
			SugFeedback sfb1=sugFeedbackMapper.selectByPrimaryKey(sfb.getId());
			OrderItems oi=orderItemsMapper.selectByPrimaryKey(sfb1.getOrderId());//查询订单
			if(oi==null){
				return new ReturnData(300,"Order does not exist.");
			}
			User u = userMapper.selectByPrimaryKey(oi.getuId());//查询用户  记录 统计用户的消费金额 与里程
			UserBrank ub = userBrankMapper.selectByPrimaryKey(oi.getUbId());//冻结预估价的卡
			if(ub==null){
				return new ReturnData(206,"Rider Payment card does not exist.");
			}
			String yearAndMon=ub.getExpirationTime();
			String[] ym=yearAndMon.split("/");
			//////支付 扣钱/////////////
			
			Map<String, String> sendMap=new HashMap<>();
	    	Integer tos=(int) (sfb.getAmountOfDeductions().doubleValue()*100);//扣款金额
	    	if(tos==0){
	    		return new ReturnData(209,"The amount must be greater than 0.");
	    	}
	    	sendMap.put("amount", tos+"");//需要支付的金额
	    	sendMap.put("currency", "AUD");
	    	sendMap.put("description", oi.getOrderNo());
	    	sendMap.put("email", u.getEmail());
	    	sendMap.put("ip_address", sfb.getCustomer_ip());
	    	sendMap.put("card[number]", ub.getUbBrankNo());
	    	sendMap.put("card[expiry_month]", ym[0]);
	    	sendMap.put("card[expiry_year]", ym[1]);
	    	sendMap.put("card[cvc]", ub.getAfterThree());
	    	sendMap.put("card[name]", ub.getTheCardholder());
//	    	sendMap.put("card[address_line1]", ub.getAddressLine1());
//	      	sendMap.put("card[address_city]", ub.getAddressCity());
	    	sendMap.put("card[address_country]", "Australia");//澳大利亚
			
			Map<String,Object> resut=PinPayHttpUtils.sendPinPayChargesPost(sendMap);//支付
	    	  if(resut.get("code")!=null&&(int)resut.get("code")==201){//支付成功
	    		  ResponseCharges rc=(ResponseCharges) resut.get("obj");
	    		   jrc = JSONObject.fromObject(rc).toString();
	 ///////////////支付结束//////////////////////	
	   			sfb.setMsgstat(3);//消息处理状态   0.未处理1.申报中2.处理中3.已处理
	   			sfb.setIsDeductions(1);//司机行程反馈使用 是否扣款 1扣款 2不扣款
	   			int num=sugFeedbackMapper.updateByPrimaryKeySelective(sfb);//更新司机的行程
	   			if(num==0){
	   				code=202;msg="Failure to modify stroke";
	   			}
	   			Map<String,Object> map = new HashMap<>();
	   			map.put("orderId", sfb1.getOrderId());
	   			List<OrderChargeDetail> list=orderChargeDetailMapper.getOrderChargeDetail(map);//订单的收费
	   			if(list.size()>0){
	   				OrderChargeDetail ocd = list.get(0);
	   				ocd.setCleanness(sfb.getAmountOfDeductions().doubleValue());
	   				ocd.setCleaning_fee_audit(2);//清洁费是否审核通过  1待审核 2审核通过 3审核不通过 
	   				Double totalFee = ocd.getTotalFee()+sfb.getAmountOfDeductions().doubleValue();
	   				ocd.setTotalFee(totalFee);//总的费用更新为有清洁费的费用
	   				ocd.setOrderDriverSettlement(ocd.getOrderDriverSettlement()+sfb.getAmountOfDeductions().doubleValue());//周末结算的费用
	   				ocd.setCancelFee(0.0);
	   				int n=orderChargeDetailMapper.updateByPrimaryKeySelective(ocd);
	   				if(n==0){
	   					code=202;msg="Update order fee details failed.";
	   				}
	   				
	   				//更新订单的支付
	   				Double totalPrice =oi.getTotalPrice().doubleValue()+sfb.getAmountOfDeductions().doubleValue();//之前付过的总的价格+清洁费
	   				
	   				BigDecimal bd = new BigDecimal(totalPrice);
	   				oi.setTotalPrice(bd);
	   				int oin=orderItemsMapper.updateByPrimaryKeySelective(oi);
	   				if(oin==0){
	   					code=203;msg="Update order failed";
	   				}
	   				UserMessage um = new UserMessage();
	   				um.setCreateTime(new Date());
	   				um.setmSummary("Please check your balance, we made a fare correction.");
	   				um.setmContext("Order number :"+oi.getOrderNo()+",Driver submitted information："+sfb1.getMsgcontent()+",Cleaning fee deduction：$ "+sfb.getAmountOfDeductions().doubleValue());
	   				um.setmName("Investigated cleaning fee claims");
	   				um.setReleaseName(user.getName());
	   				um.setType(1);//1用户端 2司机端
	   				userMessageMapper.insertSelective(um);//添加消息记录
	   			}
	   			if(code!=200){
	   				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
	   			}
	   		}else if(resut.get("code")!=null&&(int)resut.get("code")==422){
	   			ErrorResponseCard rc=(ErrorResponseCard) resut.get("obj");
	   			String error="";
	   			if(rc!=null){
	   				List<ErrorMessage> liMessage=JSONArray.toList(JSONArray.fromObject(rc.getMessages()), ErrorMessage.class);
	                for(ErrorMessage ee:liMessage){
	              	  error+=ee.getMessage()+" ; "; 
	                }
	   			}
	   			msg=jrc;
	   			jrc =  JSONObject.fromObject(rc).toString();
	   			return new ReturnData(900,error);
	   		}else if(resut.get("code")!=null&&(int)resut.get("code")==400){
	   			ErrorResponse400 rc=(ErrorResponse400) resut.get("obj");
	   			jrc = JSONObject.fromObject(rc).toString();
	   			msg=jrc;
	   			return new ReturnData(300,rc.getError_description());
			}
	      PinpayRecord ppr = new PinpayRecord(oi.getId(),jrc,(int)resut.get("code"),new Date(),1);//支付记录
  		  pinpayRecordMapper.insertSelective(ppr);//保存交易记录
  		  
		
			return new ReturnData(code,msg);
		
	}
	

	@Override
	public Object getComments(String startTime, String endTime, String telephone,Integer oa_status, Integer pageNo,
			Integer pageSize) {
		Map<String,Object> map=new HashMap<>();
		if(!StringTools.isEmpty(telephone)){
			map.put("telephone", telephone);
		}
		if(!RegexUtil.isNull(oa_status)){
			map.put("oa_status", oa_status);
		}
		if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			String[] startTime1= startTime.split("/");
	    	String[] endTime1= endTime.split("/");
	    	map.put("startTime", startTime1[2]+"-"+startTime1[1]+"-"+startTime1[0]);
	    	map.put("endTime", endTime1[2]+"-"+endTime1[1]+"-"+endTime1[0]);
	    }
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<OAStarRemark>  list=starRemarkMapper.getStarRemarkOAList(map);
			PageInfo<OAStarRemark> info = new PageInfo<>(list);
			return new ReturnData(list,info.getPages(),(int)info.getTotal(),200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
 //删除评论
	@Override
	public Object delComments(Long id) {
	try {
		int num=starRemarkMapper.deleteByPrimaryKey(id);
		if(num==0){
			return new ReturnData(201,"fail");
		}
		return new ReturnData(200,"success");
	} catch (Exception e) {
		e.printStackTrace();
		return new ReturnData(500,"Wow, that's wrong");
	}
	}

	@Override
	public Object addOrUpdateComments(String json) {
		StarRemark sr = StringTools.json2Obj(json, StarRemark.class);
		if(sr==null){
			return new ReturnData(201,"参数列表为空");
		}
		try {
			int num = starRemarkMapper.updateByPrimaryKeySelective(sr);
			if(num==0){
				return new ReturnData(201,"fail");
			}
			return new ReturnData(200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object getUserList(String startTime, String endTime, String account, Integer msgstat, Integer pageNo,
			Integer pageSize,Integer orderId) {
		Map<String,Object> map = new HashMap<>();
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=10;
		}
		if(msgstat!=null){
			map.put("msgstat", msgstat);
		}
		if(!RegexUtil.isNull(msgstat)){
			map.put("account", msgstat);
		}
		if(!RegexUtil.isNull(orderId)){
			map.put("orderId", orderId);
		}
		
		 if(!StringTools.isEmpty(startTime)&&!StringTools.isEmpty(endTime)){
			 String[] startTime1= startTime.split("/");
		    	String[] endTime1= endTime.split("/");
		    	map.put("startTime", startTime1[2]+"-"+startTime1[1]+"-"+startTime1[0]);
		    	map.put("endTime", endTime1[2]+"-"+endTime1[1]+"-"+endTime1[0]);
		    }
		 map.put("identity", 2);//司机的意见反馈
		 map.put("isUser", 1);
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<SugFeedback> list=sugFeedbackMapper.getList(map);
			PageInfo<SugFeedback> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"feedback list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}
  //不扣款
	@Override
	public Object userNorefund(Long id) {
		int code=200;String msg="success";
		try {
			SugFeedback sfb = sugFeedbackMapper.selectByPrimaryKey(id);
			sfb.setMsgstat(3);//已处理
			sfb.setIsRefund(2);
			int num = sugFeedbackMapper.updateByPrimaryKeySelective(sfb);
			if(num==0){
				code=201;msg="更新行程状态失败";
			}
			if(code!=200){
				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			}
			return new ReturnData(code,msg);
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object userSugFeedbackRefund(String json) {
		SugFeedback sfb=StringTools.json2Obj(json, SugFeedback.class);
		if(sfb==null){
			return new ReturnData(201,"参数列表为空");
		}
		try {
			if(sfb.getRefund_party()!=null&&sfb.getRefund_party()==1){//返现金 2返优惠劵
				
			}else{
				int n=sugFeedbackMapper.updateByPrimaryKeySelective(sfb);
				if(n==0){
					return new ReturnData(202,"更新失败");
				}else{
					SugFeedback sf1=sugFeedbackMapper.selectByPrimaryKey(sfb.getId());
					UserCoupon uc = new UserCoupon();
					uc.setuId(sf1.getuId()+"");
					uc.setCreateTime(new Date());
					uc.setType(1);
					uc.setCouponName("Fab rewarded coupons");
					BigDecimal bd=new BigDecimal(sfb.getRefund_amout());
					uc.setCouponMoney(bd);
					uc.setCouponOffsetMoney(bd);
					String no=userCouponMapper.getMaxNo();
					if(StringTools.isEmpty(no)){
						no="YOU00001";
					}else{
						Integer no1 =Integer.parseInt(no)+1;
						if(no1<10){
							no = "YOU0000"+no1;
						}else if(no1>=10&no1<100){
							no = "YOU000"+no1;
						}else if(no1>=100&no1<1000){
							no = "YOU00"+no1;
						}else if(no1>=1000&no1<10000){
							no = "YOU0"+no1;
						}else{
							no = "YOU"+no1;
						}
					}
					uc.setCouponNo(no);
					uc.setCouponCode(RandomUtil.getRandomNumberNozeroStart(11));
					int num=userCouponMapper.insertSelective(uc);
				}
			}
			return new ReturnData(201,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdateClean(String json) {
		OrderChargeDetail ocd = StringTools.json2Obj(json, OrderChargeDetail.class);
		if(ocd==null){
			return new ReturnData(201,"List must be filled！");
		}
		try {
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public Object refund(String json,HttpSession session) {
		SugFeedback sfb = StringTools.json2Obj(json, SugFeedback.class);
		if(sfb==null){
			return new ReturnData(201,"List must be filled");
		}
		int code=200;String msg="success";
		SysUser user = (SysUser) session.getAttribute("user");
		if(user==null){
			return new ReturnData(205,"Login expired, please log in again.");
		}	
		String jrc =null;
//		try {
			SugFeedback sfb1=sugFeedbackMapper.selectByPrimaryKey(sfb.getId());
			OrderItems oi=orderItemsMapper.selectByPrimaryKey(sfb1.getOrderId());//查询订单
			if(oi==null){
				return new ReturnData(300,"Order does not exist.");
			}
			User u = userMapper.selectByPrimaryKey(oi.getuId());//查询用户  记录 统计用户的消费金额 与里程
			if(sfb.getRefund_party()==1){
				UserBrank ub = userBrankMapper.selectByPrimaryKey(oi.getUbId());//冻结预估价的卡
				if(ub==null){
					return new ReturnData(206,"Rider Payment card does not exist.");
				}
				String yearAndMon=ub.getExpirationTime();
				String[] ym=yearAndMon.split("/");
				//////支付 扣钱/////////////
				
				Map<String, String> sendMap=new HashMap<>();
		    	Integer tos=(int) (sfb.getRefund_amout().doubleValue()*100);//扣款金额
		    	if(tos==0){
		    		return new ReturnData(209,"The amount must be greater than 0.");
		    	}
		    	sendMap.put("amount", tos+"");//需要支付的金额
		    	sendMap.put("currency", "AUD");
		    	sendMap.put("description", sfb.getRefund_reason());
		    	sendMap.put("email", u.getEmail());
		    	sendMap.put("ip_address", sfb.getCustomer_ip());
		    	sendMap.put("card[number]", ub.getUbBrankNo());
		    	sendMap.put("card[expiry_month]", ym[0]);
		    	sendMap.put("card[expiry_year]", ym[1]);
		    	sendMap.put("card[cvc]", ub.getAfterThree());
		    	sendMap.put("card[name]", ub.getTheCardholder());
		    	sendMap.put("card[address_country]", "Australia");//澳大利亚
				Map<String,Object> resut=PinPayHttpUtils.sendPinPayChargesPost(sendMap);//支付
		    	  if(resut.get("code")!=null&&(int)resut.get("code")==201){//支付成功
		    		  ResponseCharges rc=(ResponseCharges) resut.get("obj");
		    		   jrc = JSONObject.fromObject(rc).toString();
		 ///////////////支付结束//////////////////////	
		    		   sfb.setIsRefund(1);//用户行程反馈  是否退款 1退款 2不退款
		   			int num=sugFeedbackMapper.updateByPrimaryKeySelective(sfb);//更新司机的行程
		   			if(num==0){
		   				code=202;msg="Failure to modify stroke";
		   			}
		   			Map<String,Object> map = new HashMap<>();
		   			map.put("orderId", sfb1.getOrderId());
		   			List<OrderChargeDetail> list=orderChargeDetailMapper.getOrderChargeDetail(map);//订单的收费
		   			if(list.size()>0){
		   				OrderChargeDetail ocd = list.get(0);
		   				ocd.setOtherFee(sfb.getRefund_amout().doubleValue());//退款费用
		   				Double totalFee = ocd.getTotalFee()-sfb.getRefund_amout().doubleValue();
		   				ocd.setTotalFee(totalFee);//总的费用更新为有清洁费的费用
		   				ocd.setOrderDriverSettlement(ocd.getOrderDriverSettlement()-sfb.getRefund_amout().doubleValue());//周末结算的费用
		   				int n=orderChargeDetailMapper.updateByPrimaryKeySelective(ocd);
		   				if(n==0){
		   					code=202;msg="Update order fee details failed.";
		   				}
		   				
		   				//更新订单的支付
		   				Double totalPrice =oi.getTotalPrice().doubleValue()-sfb.getRefund_amout().doubleValue();//之前付过的总的价格-退款
		   				
		   				BigDecimal bd = new BigDecimal(totalPrice);
		   				oi.setTotalPrice(bd);
		   				int oin=orderItemsMapper.updateByPrimaryKeySelective(oi);
		   				if(oin==0){
		   					code=203;msg="Update order failed";
		   				}
		   			}
		   			if(code!=200){
		   				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
		   			}
		   		}else if(resut.get("code")!=null&&(int)resut.get("code")==422){
		   			ErrorResponseCard rc=(ErrorResponseCard) resut.get("obj");
		   			String error="";
		   			if(rc!=null){
		   				List<ErrorMessage> liMessage=JSONArray.toList(JSONArray.fromObject(rc.getMessages()), ErrorMessage.class);
		                for(ErrorMessage ee:liMessage){
		              	  error+=ee.getMessage()+" ; "; 
		                }
		   			}
		   			msg=jrc;
		   			jrc =  JSONObject.fromObject(rc).toString();
		   			return new ReturnData(900,error);
		   		}else if(resut.get("code")!=null&&(int)resut.get("code")==400){
		   			ErrorResponse400 rc=(ErrorResponse400) resut.get("obj");
		   			jrc = JSONObject.fromObject(rc).toString();
		   			msg=jrc;
		   			return new ReturnData(300,rc.getError_description());
				}
		      PinpayRecord ppr = new PinpayRecord(oi.getId(),jrc,(int)resut.get("code"),new Date(),1);//支付记录
	  		  pinpayRecordMapper.insertSelective(ppr);//保存交易记录
			}else{
				sfb.setIsRefund(1);
				int num=sugFeedbackMapper.updateByPrimaryKeySelective(sfb);//更新司机的行程
	   			if(num==0){
	   				code=202;msg="Failure to modify stroke";
	   			}
	   			UserCoupon uc = new UserCoupon();
				uc.setuId(oi.getuId()+"");
				uc.setCreateTime(new Date());
				uc.setType(1);
				uc.setCouponName("Fab rewarded coupons");
				BigDecimal bd=new BigDecimal(sfb.getRefund_amout());
				uc.setCouponMoney(bd);
				uc.setCouponOffsetMoney(bd);
				String no=userCouponMapper.getMaxNo();
				if(StringTools.isEmpty(no)){
					no="YOU00001";
				}else{
					Integer no1 =Integer.parseInt(no)+1;
					if(no1<10){
						no = "YOU0000"+no1;
					}else if(no1>=10&no1<100){
						no = "YOU000"+no1;
					}else if(no1>=100&no1<1000){
						no = "YOU00"+no1;
					}else if(no1>=1000&no1<10000){
						no = "YOU0"+no1;
					}else{
						no = "YOU"+no1;
					}
				}
				uc.setCouponNo(no);
				uc.setCouponCode(RandomUtil.getRandomNumberNozeroStart(11));
				int num1=userCouponMapper.insertSelective(uc);
			}
			return new ReturnData(code,msg);
		
	}

}
